<?php

namespace addons\yunfood\controller;

use addons\yunfood\library\Logic;
use addons\yunfood\library\Super;
use addons\yunfood\model\Question;
use addons\yunfood\model\Page;

/**
 * 内容接口
 */
class Content extends BaseApi
{
    public $noNeedLogin = ['getPageList', 'getPageDetail', 'getQuestionList', 'getQuestionDetail'];

    // 获取单页列表
    public function getPageList()
    {
        if (! $position = $this->request->post('position')) {
            $this->error('position不能为空');
        }

        // 数据列表
        $dataList = Page::where(['position' => $position, 'status' => Page::STATUS_NORMAL])
            ->order(['weigh' => 'ASC', 'id' => 'DESC'])->select();

        $result = Super::arrayGetSet($dataList, [
            'id',
            'name',
        ], true);

        $this->success('success', $result);
    }

    // 获取单页详情
    public function getPageDetail()
    {
        $pageId = $this->request->post('id');
        if (! $page = Page::get(['id' => $pageId])) {
            $this->error('单页不存在');
        }

        $result = Super::arrayGetSet($page, [
            'id',
            'name',
            'content',
            'created_at',
        ]);

        $this->success('success', $result);
    }

    // 获取常见问题列表
    public function getQuestionList()
    {
        // 数据列表
        $query = Question::where(['status' => Question::STATUS_NORMAL])
            ->order(['weigh' => 'ASC', 'id' => 'DESC']);

        $result = $query->paginate($this->request->post('page_size', 10), false);
        $result = Logic::getPageData($result, [
            'id',
            'title',
            'publish_time',
        ]);

        $this->success('success', $result);
    }

    // 获取常见问题详情
    public function getQuestionDetail()
    {
        $questionId = $this->request->post('id');
        if (! $question = Question::get(['id' => $questionId])) {
            $this->error('问题不存在');
        }

        $result = Super::arrayGetSet($question, [
            'id',
            'title',
            'content',
            'publish_time',
        ]);

        $this->success('success', $result);
    }
}
